<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="讲师" prop="teacherId">
        <!-- <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入讲师id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.teacherId" placeholder="请选择" 
          filterable 
          allow-create 
          default-first-option>
          <el-option
            v-for="item in teacheoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级">
          <el-option
            v-for="item in clazzoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        
      </el-form-item>
      <el-form-item label="日期" prop="dailyDate">
        <el-date-picker 
        v-model="queryParams.dailyDate"
        type="date" 
        format="yyyy-MM-dd" 
        clearable
        value-format="yyyy-MM-dd" 
        placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['perfomance:daily:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['perfomance:daily:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['perfomance:daily:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['perfomance:daily:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dailyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="讲师" align="center" prop="tname" />
      <el-table-column label="班级" align="center" prop="cname" />
      <el-table-column label="当前授课章节" align="center" prop="chapter" />
      <el-table-column label="授课问题" align="center" prop="problomTxt" />
      <el-table-column label="作业内容" align="center" prop="taskTxt" />
      <el-table-column label="日期" align="center" prop="dailyDate" />
      <el-table-column label="课时" align="center" prop="dailyCount" />
      <el-table-column label="请假人数" align="center" prop="leavecount" />
      <el-table-column label="请假人员" align="center" prop="leavename" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['perfomance:daily:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['perfomance:daily:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日报管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="讲师id" prop="teacherId">
          <!-- <el-input v-model="form.teacherId" placeholder="请输入讲师id" /> -->
           <el-select v-model="form.teacherId" placeholder="请选择" 
            filterable 
            allow-create 
            default-first-option>
            <el-option
              v-for="item in teacheoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              >
            </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="班级id" prop="classId">

          <el-select v-model="form.classId" placeholder="请选择班级"           
            filterable 
            allow-create 
            default-first-option>
          <el-option
            v-for="item in clazzoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="当前授课章节" prop="chapterId">
          <el-input v-model="form.chapter" readonly placeholder="请输入当前授课章节id" />
           <svg-icon class-name="search-icon" icon-class="search" @click.stop.prevent="handleshowchapter" />
        </el-form-item>
        <el-form-item label="授课问题" prop="problomTxt">
          <el-input v-model="form.problomTxt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="作业内容" prop="taskTxt">
          <el-input v-model="form.taskTxt" placeholder="请输入作业内容" />
        </el-form-item>
        <el-form-item label="日期" prop="dailyDate">
           <el-date-picker 
              v-model="form.dailyDate"
              type="date"
              format="yyyy-MM-dd" 
              clearable
              value-format="yyyy-MM-dd" 
              placeholder="选择日期"
              >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="课时" prop="dailyCount">
          <el-input v-model="form.dailyCount" placeholder="请输入课时" />
        </el-form-item>
        <el-form-item label="请假人数" prop="leavecount">
          <el-input v-model="form.leavecount" placeholder="请输入请假人数" />
        </el-form-item>
        <el-form-item label="请假人员" prop="leavename">
          <el-input v-model="form.leavename" placeholder="请输入请假人员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
     <!-- 修改章节对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules" label-width="80px">
        <combox-liandong ref="form2" v-model="defultAddress"></combox-liandong>
        <!-- <el-form-item label="请假人员" prop="leavename">
          <el-input v-model="form.leavename" placeholder="请输入请假人员" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormfatcher">确 定</el-button>
        <el-button @click="cancelFormfatcher">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDaily, getDaily, delDaily, addDaily, updateDaily, exportDaily } from "@/api/perfomance/daily";
import { comboxTeacher, comboxClazz } from '@/api/combox'
import ComboxLiandong from '@/components/comboxliandong'
export default {
 components : { ComboxLiandong },
  name: "Daily",
  data() {
    return {

      defultAddress: {
        sheng: 'Java',
        shi: '',
        qu: ''
      },
      form1: {},
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日报管理表格数据
      dailyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
            // 是否显示弹出层
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: '',
        classId: null,
        chapterId: null,
        dailyDate: new Date(),
      },
      // 表单参数
      form: {
        chapteris: [],
        chapter: ''
      },
      teacheoptions: [],
      clazzoptions: [],
      // 表单校验
      rules: {
      }
    };
  },
  created() {

    console.info(this.$store.getters.user)
    comboxTeacher().then(res => {
      this.teacheoptions = res
    })
    comboxClazz().then(res => {
      this.clazzoptions = res
    })
    this.getList();
  },
  methods: {
    abc() {
      console.info(arguments)
    },
    /** 查询日报管理列表 */
    getList() {
      this.loading = true;
      listDaily(this.queryParams).then(response => {
        this.dailyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.defultAddress= {
        sheng: 'Java',
        shi: '',
        qu: ''
      },
      this.form = {
        id: null,
        teacherId: null,
        classId: null,
        chapterId: null,
        problomTxt: null,
        taskTxt: null,
        dailyDate: null,
        dailyCount: null,
        leavecount: null,
        leavename: null,
        chapter: '',
        chapteris: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加日报管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      const id = row.id || this.ids
      getDaily(id).then(response => {
        this.form = response.data;
        if (response.data.chapter) {
          let datas = response.data.chapter.split("|")
          this.defultAddress = {
            sheng: datas[0],
            shi: datas[1],
            qu: datas[2]
          }
        }
        this.open = true;
        this.title = "修改日报管理";
      });
    },

    /** 选择授课章节页面  */
    cancelFormfatcher() {
        this.open1 = false;
    },
    submitFormfatcher() {

      this.open1 = false;
      let str = ''
      let chapteris = []
      for (let pro in this.$refs.form2.selectcode) {
        str = str + this.$refs.form2.selectcode[pro].name + "|"
        chapteris.push(this.$refs.form2.selectcode[pro].code)
        if (pro == 0) {
            this.form.dirctory = this.$refs.form2.selectcode[pro].code
        } else if (pro == 1) {
            this.form.stageid = this.$refs.form2.selectcode[pro].code 
        } else if (pro == 2) {
           this.form.chapterId = this.$refs.form2.selectcode[pro].code
        }
      }
      this.form.chapter = str.substring(0, str.lastIndexOf('|'))
      this.form.chapteris = chapteris
    },
    /** 修改章节联动 */
    handleshowchapter(row) {
      this.open1 = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            console.info(this.form)
            updateDaily(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDaily(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除日报管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDaily(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有日报管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDaily(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
